package com.other.src.main.java.com.kuang.cas;

import java.util.concurrent.TimeUnit;

public class DeadLock {

    public static void main(String[] args) {
        new Thread(new MyRunnable("LockA","LockB"),"A").start();
        new Thread(new MyRunnable("LockB","LockA"),"B").start();
    }
}
 class MyRunnable implements Runnable {

    private String LockA;
    private String LockB;

    public MyRunnable(String lockA, String lockB) {
        LockA = lockA;
        LockB = lockB;
    }


     @Override
     public void run() {

        synchronized (LockA){
            System.out.println(Thread.currentThread().getName()+"lock: " + LockA +" => get"+LockB);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (LockB){
                System.out.println(Thread.currentThread().getName()+"lock: " + LockB +" => get"+LockA);
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

     }
 }